<template>
	<div>
		
		
		
		
		<el-form ref="form" :model="form" label-width="80px">
		  <el-form-item label="用户名">
		    <el-input v-model="form.username"></el-input>
		  </el-form-item>
		  
			<el-form-item label="部门">
			  <!-- <el-input v-model="form.deptId"></el-input> -->
			  
			  
			  <el-select v-model="form.deptId" placeholder="请选择部门">
			      <el-option
			        v-for="item in depts"
			        :key="item.id"
			        :label="item.name"
			        :value="item.id">
			      </el-option>
			    </el-select>
			  
			  
			</el-form-item>
			<el-form-item label="医生级别">
			  <!-- <el-input v-model="form.deptId"></el-input> -->
			  
			  
			  <el-select v-model="form.levelId" placeholder="请选择级别">
			      <el-option
			        v-for="item in registLevels"
			        :key="item.id"
			        :label="item.name"
			        :value="item.id">
			      </el-option>
			    </el-select>
			  
			  
			</el-form-item>
			
			<el-form-item label="性别">
				<el-radio v-model="form.gender"  v-for="item in genders"  :label="item.code">{{item.name}}  -- {{item.code}}</el-radio>
			</el-form-item>
		  
		  <el-form-item>
		    <el-button type="primary" @click="onSubmit">保存</el-button>
		  </el-form-item>
		</el-form>
		
		
		
		
		
		
	</div>
</template>

<script>
	
	export default {
		name:'UserEdit',
		props:["id"],
		data(){
			return{
				form:{
					username:'',
					deptId:'',
					gender:'',
					levelId:''
				},
				
				// {
				//          id: '选项1',
				//          name: '黄金糕'
				//        }, {
				//          id: '选项2',
				//          name: '双皮奶'
				//        }, {
				//          id: '选项3',
				//          name: '蚵仔煎'
				//        }, {
				//          id: '选项4',
				//          name: '龙须面'
				//        }, {
				//          id: '选项5',
				//          name: '北京烤鸭'
				//        }
				 depts: [],
				 registLevels: [],
				 genders:[
					 { code:'1',name:'男'},
					 { code:'2',name:'女'},
				 ]
			}
		},
		methods:{
			onSubmit(){
				//校验表单，
				
				// 提交 表单 post
				
				this.http.post('/api/user/save',(success)=>{
					// alert(success)
					if(success){
						 this.$message({
							message: '保存成功',
							type: 'success'
						});
						this.$emit("update:visible",false); //关闭对话框
						this.$emit("flush"); //发起刷新事件
					}
				},this.form)
				
			},
			//修改的时候初始化页面
			init(){
				
				this.http.get("/api/department/list",(depts)=>{
					this.depts = depts; //页面上包含部门列表 
				})
				
				this.http.get("/api/registLevel/list",(registLevels)=>{
					this.registLevels = registLevels; //页面上包含部门列表 
				})
				
				if(this.id){ //如果有id则需要查询对应的数据
					this.http.get('/api/user/getById',(user)=>{
						this.form = user;
					
					},{id:this.id})
				}
			}
		},
		created() {
			
			this.init()
		}
		
	}
</script>

<style>
</style>
